<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>个人中心 - 猫咖管理系统</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <style>
        :root {
            --primary-color: #D85D5D;
            --secondary-color: #9B7E46;
            --light-color: #F8F3EA;
            --dark-color: #333333;
            --success-color: #4CAF50;
            --error-color: #FF5252;
            --transition: all 0.3s ease;
            --shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background-color: #f9f9f9;
            color: var(--dark-color);
            line-height: 1.6;
            background: linear-gradient(135deg, var(--light-color) 0%, #f9f9f9 100%);
            min-height: 100vh;
        }

        /* 顶部导航栏样式 */
        header {
            background-color: white;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            position: sticky;
            top: 0;
            z-index: 1000;
        }

        .header-container {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 15px 20px;
            max-width: 1200px;
            margin: 0 auto;
        }

        .logo {
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .logo-icon {
            width: 40px;
            height: 40px;
            background-color: var(--primary-color);
            border-radius: 50%;
            position: relative;
        }

        .logo-icon::after {
            content: '';
            position: absolute;
            top: -4px;
            left: -4px;
            width: 48px;
            height: 48px;
            border-radius: 50%;
            border: 1px solid rgba(216, 93, 93, 0.2);
        }

        .logo h1 {
            font-size: 24px;
            color: var(--primary-color);
        }

        nav ul {
            display: flex;
            list-style: none;
            gap: 20px;
        }

        nav a {
            text-decoration: none;
            color: var(--dark-color);
            font-weight: 500;
            padding: 8px 12px;
            border-radius: 4px;
            transition: var(--transition);
            position: relative;
        }

        nav a:hover,
        nav a.active {
            color: var(--primary-color);
        }

        nav a::after {
            content: '';
            position: absolute;
            bottom: -5px;
            left: 0;
            width: 0;
            height: 2px;
            background-color: var(--primary-color);
            transition: var(--transition);
        }

        nav a:hover::after,
        nav a.active::after {
            width: 100%;
        }

        .user-info {
            display: flex;
            align-items: center;
            gap: 15px;
        }

        .user-info span {
            font-weight: 500;
        }

        .btn {
            padding: 8px 16px;
            border-radius: 4px;
            border: none;
            cursor: pointer;
            font-weight: 500;
            transition: var(--transition);
        }

        .btn-primary {
            background-color: var(--primary-color);
            color: white;
        }

        .btn-primary:hover {
            background-color: #c74d4d;
            transform: translateY(-2px);
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
        }

        .btn-outline {
            background-color: transparent;
            border: 2px solid var(--primary-color);
            color: var(--primary-color);
        }

        .btn-outline:hover {
            background-color: var(--primary-color);
            color: white;
        }

        .btn-icon {
            display: flex;
            align-items: center;
            gap: 8px;
        }

        /* 主要内容区域 */
        .main-container {
            max-width: 1200px;
            margin: 40px auto;
            padding: 0 20px;
            display: grid;
            grid-template-columns: 1fr;
            gap: 30px;
        }

        .profile-section {
            display: grid;
            grid-template-columns: 1fr;
            gap: 30px;
        }

        @media (min-width: 900px) {
            .profile-section {
                grid-template-columns: 300px 1fr;
            }
        }

        .profile-card {
            background-color: white;
            border-radius: 12px;
            box-shadow: var(--shadow);
            padding: 30px;
            text-align: center;
            display: flex;
            flex-direction: column;
            align-items: center;
            height: fit-content;
        }

        .profile-image {
            width: 180px;
            height: 180px;
            border-radius: 50%;
            object-fit: cover;
            border: 4px solid var(--light-color);
            margin: 0 auto 20px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }

        .profile-name {
            font-size: 28px;
            color: var(--primary-color);
            margin-bottom: 10px;
        }

        .profile-role {
            display: inline-block;
            padding: 6px 16px;
            background-color: var(--light-color);
            border-radius: 20px;
            color: var(--secondary-color);
            font-size: 16px;
            margin-bottom: 25px;
        }

        .points-container {
            background: linear-gradient(135deg, #FFA384 0%, var(--primary-color) 100%);
            border-radius: 12px;
            padding: 20px;
            width: 100%;
            margin: 15px 0 25px;
            color: white;
            box-shadow: 0 4px 12px rgba(216, 93, 93, 0.3);
        }

        .points-label {
            font-size: 18px;
            margin-bottom: 8px;
        }

        .points-value {
            font-size: 42px;
            font-weight: bold;
            line-height: 1;
        }

        .edit-btn {
            width: 100%;
            padding: 12px;
            font-size: 18px;
            margin-top: 10px;
        }

        .info-card {
            background-color: white;
            border-radius: 12px;
            box-shadow: var(--shadow);
            padding: 30px;
        }

        .section-title {
            margin-bottom: 30px;
            padding-bottom: 15px;
            border-bottom: 2px solid var(--light-color);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .section-title h2 {
            font-size: 28px;
            color: var(--primary-color);
            display: flex;
            align-items: center;
            gap: 12px;
        }

        .section-title h2 i {
            color: var(--secondary-color);
        }

        .info-grid {
            display: grid;
            grid-template-columns: 1fr;
            gap: 20px;
        }

        @media (min-width: 600px) {
            .info-grid {
                grid-template-columns: repeat(2, 1fr);
            }
        }

        .info-item {
            display: flex;
            margin-bottom: 25px;
            padding-bottom: 15px;
            border-bottom: 1px dashed #eee;
        }

        .info-label {
            font-weight: 600;
            min-width: 100px;
            color: var(--secondary-color);
        }

        .info-value {
            color: var(--dark-color);
            word-break: break-word;
            font-size: 18px;
        }

        /* 模态框样式 */
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            z-index: 2000;
            align-items: center;
            justify-content: center;
        }

        .modal-content {
            background-color: white;
            border-radius: 12px;
            width: 90%;
            max-width: 500px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
            overflow: hidden;
            animation: modalAppear 0.4s ease;
        }

        @keyframes modalAppear {
            from {
                opacity: 0;
                transform: translateY(-30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .modal-header {
            background-color: var(--primary-color);
            color: white;
            padding: 20px;
            position: relative;
        }

        .modal-header h3 {
            font-size: 22px;
        }

        .close-btn {
            position: absolute;
            top: 20px;
            right: 20px;
            font-size: 24px;
            cursor: pointer;
            transition: var(--transition);
        }

        .close-btn:hover {
            transform: rotate(90deg);
        }

        .modal-body {
            padding: 25px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: var(--dark-color);
        }

        .form-control {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 8px;
            font-size: 16px;
            transition: var(--transition);
        }

        .form-control:focus {
            border-color: var(--primary-color);
            outline: none;
            box-shadow: 0 0 0 3px rgba(216, 93, 93, 0.2);
        }

        .form-control[readonly] {
            background-color: #f5f5f5;
            cursor: not-allowed;
        }

        .avatar-preview {
            text-align: center;
            margin: 15px 0;
        }

        .avatar-preview img {
            width: 120px;
            height: 120px;
            border-radius: 50%;
            object-fit: cover;
            border: 3px solid var(--light-color);
            box-shadow: 0 3px 10px rgba(0, 0, 0, 0.1);
        }

        .modal-footer {
            display: flex;
            justify-content: flex-end;
            gap: 15px;
            padding: 20px;
            border-top: 1px solid #eee;
        }

        /* 页脚样式 */
        footer {
            background-color: var(--dark-color);
            color: white;
            padding: 40px 0 20px;
            margin-top: 60px;
        }

        .footer-content {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 20px;
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 30px;
            margin-bottom: 30px;
        }

        .footer-column h3 {
            font-size: 20px;
            margin-bottom: 20px;
            position: relative;
            padding-bottom: 10px;
        }

        .footer-column h3::after {
            content: '';
            position: absolute;
            bottom: 0;
            left: 0;
            width: 50px;
            height: 2px;
            background-color: var(--primary-color);
        }

        .footer-links {
            list-style: none;
        }

        .footer-links li {
            margin-bottom: 10px;
        }

        .footer-links a {
            color: #bbb;
            text-decoration: none;
            transition: var(--transition);
        }

        .footer-links a:hover {
            color: white;
            padding-left: 5px;
        }

        .contact-info {
            color: #bbb;
        }

        .contact-info p {
            margin-bottom: 10px;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .copyright {
            text-align: center;
            padding-top: 20px;
            border-top: 1px solid #444;
            color: #bbb;
            font-size: 14px;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .header-container {
                flex-wrap: wrap;
                gap: 15px;
            }

            nav ul {
                flex-wrap: wrap;
                justify-content: center;
            }

            .profile-name {
                font-size: 24px;
            }

            .points-value {
                font-size: 36px;
            }
        }
    </style>
</head>
<body>
<!-- 顶部导航栏 -->
<header>
    <div class="header-container">
        <div class="logo">
            <div class="logo-icon"></div>
            <h1>猫咖管理系统</h1>
        </div>

        <nav>
            <ul>
                <li><a href="index.html">首页</a></li>
                <li><a href="cat.html">猫咪预约</a></li>
                <li><a href="cafe.html">咖啡购买</a></li>
                <li><a href="center.html" class="active">个人中心</a></li>
                <li><a href="message.html">留言板</a></li>
                <li><a href="orders.html">订单信息</a></li>
                <li><a href="booking.html">预约信息</a></li>
<!--                <li><a href="shoppingcart.html">购物车</a></li>-->
            </ul>
        </nav>

        <div class="user-info">
            <span><i class="fas fa-user-circle"></i> 未登录</span>
            <button class="btn btn-primary" id="logout-btn">退出</button>
        </div>
    </div>
</header>

<!-- 主要内容区域 -->
<div class="main-container">
    <div class="profile-section">
        <!-- 左侧个人信息卡片 -->
        <div class="profile-card">
            <img src="https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?q=80&w=1780&auto=format&fit=crop"
                 alt="用户头像" class="profile-image" id="profileImage">
            <div class="profile-name" id="profileName">会非的杨</div>
            <div class="profile-role" id="profileRole">管理员</div>

            <div class="points-container">
                <div class="points-label">我的积分</div>
                <div class="points-value" id="pointsValue">1,250</div>
            </div>

            <button class="btn btn-primary btn-icon edit-btn" id="editProfileBtn">
                <i class="fas fa-edit"></i> 编辑资料
            </button>
        </div>

        <!-- 右侧详细信息区域 -->
        <div class="info-card">
            <div class="section-title">
                <h2><i class="fas fa-user"></i> 个人信息</h2>
                <button class="btn btn-outline btn-icon" id="editInfoBtn">
                    <i class="fas fa-edit"></i> 修改信息
                </button>
            </div>

            <div class="info-grid">
                <div class="info-item">
                    <div class="info-label">用户名：</div>
                    <div class="info-value" id="usernameValue">yanghuifei</div>
                </div>

                <div class="info-item">
                    <div class="info-label">用户角色：</div>
                    <div class="info-value" id="roleValue">管理员</div>
                </div>

                <div class="info-item">
                    <div class="info-label">昵称：</div>
                    <div class="info-value" id="nicknameValue">会非的杨</div>
                </div>

                <div class="info-item">
                    <div class="info-label">手机号：</div>
                    <div class="info-value" id="phoneValue">13800138000</div>
                </div>

                <div class="info-item">
                    <div class="info-label">注册时间：</div>
                    <div class="info-value">2024-03-15</div>
                </div>

                <div class="info-item">
                    <div class="info-label">最后登录：</div>
                    <div class="info-value">2025-06-18 14:30</div>
                </div>
            </div>
        </div>
    </div>
</div>

<!-- 编辑模态框 -->
<div class="modal" id="editModal">
    <div class="modal-content">
        <div class="modal-header">
            <h3><i class="fas fa-user-edit"></i> 编辑个人信息</h3>
            <span class="close-btn" id="closeModalBtn">&times;</span>
        </div>
        <div class="modal-body">
            <div class="avatar-preview">
                <img src="https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?q=80&w=1780&auto=format&fit=crop"
                     alt="头像预览" id="avatarPreview">
            </div>

            <div class="form-group">
                <label for="usernameInput">用户名</label>
                <input type="text" id="usernameInput" class="form-control" readonly>
            </div>

            <div class="form-group">
                <label for="roleInput">用户角色</label>
                <input type="text" id="roleInput" class="form-control" readonly>
            </div>

            <div class="form-group">
                <label for="nicknameInput">昵称</label>
                <input type="text" id="nicknameInput" class="form-control" placeholder="请输入您的昵称">
            </div>

            <div class="form-group">
                <label for="phoneInput">手机号</label>
                <input type="tel" id="phoneInput" class="form-control" placeholder="请输入您的手机号">
            </div>

            <div class="form-group">
                <label for="avatarInput">更换头像</label>
                <input type="file" id="avatarInput" class="form-control" accept="image/*">
                <small class="form-text text-muted">支持 JPG, PNG 格式，最大 2MB</small>
            </div>
        </div>
        <div class="modal-footer">
            <button class="btn btn-outline" id="cancelEditBtn">取消</button>
            <button class="btn btn-primary" id="saveChangesBtn">保存更改</button>
        </div>
    </div>
</div>

<!-- 页脚 -->
<footer>
    <div class="footer-content">
        <div class="footer-column">
            <h3>关于我们</h3>
            <p>猫咖管理系统致力于为猫咪咖啡馆提供专业的管理解决方案，帮助店主高效管理猫咪、咖啡和顾客信息。</p>
        </div>

        <div class="footer-column">
            <h3>快速链接</h3>
            <ul class="footer-links">
                <li><a href="index.html">首页</a></li>
                <li><a href="cat.html">猫咪信息</a></li>
                <li><a href="cafe.html">咖啡菜单</a></li>
                <li><a href="">在线预订</a></li>
                <li><a href="center.html">个人中心</a></li>
            </ul>
        </div>

        <div class="footer-column">
            <h3>联系方式</h3>
            <div class="contact-info">
                <p><i class="fas fa-map-marker-alt"></i> 地址：广东科技学院</p>
                <p><i class="fas fa-phone"></i> 电话：666666</p>
                <p><i class="fas fa-envelope"></i> 邮箱：666666.com</p>
                <p><i class="fas fa-clock"></i> 营业时间：10:00-18:00</p>
            </div>
        </div>
    </div>

    <div class="copyright">
        <p>© 2025 猫咖管理系统 版权所有 | 设计开发：会非的杨</p>
    </div>
</footer>

<script>
    // 页面加载完成后执行
    document.addEventListener('DOMContentLoaded', function() {
        // 获取用户信息元素
        const profileImage = document.getElementById('profileImage');
        const profileName = document.getElementById('profileName');
        const profileRole = document.getElementById('profileRole');
        const pointsValue = document.getElementById('pointsValue');
        const usernameValue = document.getElementById('usernameValue');
        const roleValue = document.getElementById('roleValue');
        const nicknameValue = document.getElementById('nicknameValue');
        const phoneValue = document.getElementById('phoneValue');

        // 获取模态框元素
        const editModal = document.getElementById('editModal');
        const closeModalBtn = document.getElementById('closeModalBtn');
        const cancelEditBtn = document.getElementById('cancelEditBtn');
        const saveChangesBtn = document.getElementById('saveChangesBtn');
        const editProfileBtn = document.getElementById('editProfileBtn');
        const editInfoBtn = document.getElementById('editInfoBtn');

        // 获取表单元素
        const usernameInput = document.getElementById('usernameInput');
        const roleInput = document.getElementById('roleInput');
        const nicknameInput = document.getElementById('nicknameInput');
        const phoneInput = document.getElementById('phoneInput');
        const avatarInput = document.getElementById('avatarInput');
        const avatarPreview = document.getElementById('avatarPreview');

        // API 端点配置
        const API_BASE_URL = 'http://localhost:8080/catcafe';

        // 从localStorage获取用户信息
        const username = localStorage.getItem('username');
        const role = localStorage.getItem('role');

        // 获取显示用户信息的元素
        const userDisplay = document.querySelector('.user-info span');

        // 如果有用户信息，显示在导航栏
        if (username) {
            // 根据角色显示不同的文本
            const roleText = role === 'admin' ? '管理员' : '普通用户';
            userDisplay.innerHTML = `            <i class="fas fa-user-circle"></i> ${roleText}：${username}        `;
        } else {
            userDisplay.innerHTML = '<i class="fas fa-user-circle"></i> 未登录';
        }

        // 退出按钮功能
        const logoutBtn = document.getElementById('logout-btn');
        if (logoutBtn) {
            logoutBtn.addEventListener('click', function () {
                // 清除存储的用户信息
                localStorage.removeItem('username');
                localStorage.removeItem('role');

                // 提示用户并跳转到登录页
                alert('您已成功退出');
                window.location.href = 'login.html'; // 替换为你的登录页面路径
            });
        }

// 页面加载时获取用户信息
        function fetchUserInfo() {
            const username = localStorage.getItem('username'); // 从 localStorage 获取 username
            if (!username) {
                alert('未找到登录信息，请重新登录');
                window.location.href = 'login.html'; // 跳转到登录页
                return;
            }

            const url = `${API_BASE_URL}/user/profile?username=${encodeURIComponent(username)}`;

            fetch(url)
                .then(response => {
                    if (!response.ok) throw new Error('网络响应失败');
                    return response.json();
                })
                .then(data => {
                    if (data.code === 1) {
                        const user = data.data;
                        updateUIWithUserData(user);
                    } else {
                        alert('获取用户信息失败: ' + (data.msg || '未知错误'));
                    }
                })
                .catch(error => {
                    console.error('获取用户信息失败:', error);
                    alert('无法连接服务器，请检查网络');
                });
        }


// 更新 UI
        function updateUIWithUserData(user) {
            profileImage.src = user.image || 'https://images.unsplash.com/photo-1514432324607-a09d9b4aefdd?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=774&q=80';
            profileName.textContent = user.nickname || '暂无昵称';
            profileRole.textContent = user.role || '普通用户';
            pointsValue.textContent = user.points || '0';
            usernameValue.textContent = user.username || '未登录';
            roleValue.textContent = user.role || '普通用户';
            nicknameValue.textContent = user.nickname || '未设置';
            phoneValue.textContent = user.phone || '未绑定';

            // 模态框数据初始化
            usernameInput.value = user.username || '';
            roleInput.value = user.role || '';
            nicknameInput.value = user.nickname || '';
            phoneInput.value = user.phone || '';
            avatarPreview.src = user.image || 'https://images.unsplash.com/photo-1514432324607-a09d9b4aefdd?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D&auto=format&fit=crop&w=774&q=80';
        }

// 初始化页面
        fetchUserInfo();


        // 打开模态框
        function openEditModal() {
            editModal.style.display = "flex";
            document.body.style.overflow = "hidden";
        }

        // 关闭模态框
        function closeEditModal() {
            editModal.style.display = "none";
            document.body.style.overflow = "auto";
        }

        function saveChanges() {
            const updatedData = {
                nickname: nicknameInput.value.trim(),
                phone: phoneInput.value.trim()
            };

            const username = localStorage.getItem('username'); // 从 localStorage 获取 username
            if (!username) {
                alert('未找到登录信息，请重新登录');
                window.location.href = 'login.html'; // 跳转到登录页
                return;
            }

            // 图片上传处理
            const file = avatarInput.files[0];
            const formData = new FormData();

            // 添加文本字段
            formData.append('nickname', updatedData.nickname);
            formData.append('phone', updatedData.phone);
            formData.append('username', username); // ← 新增这一行，添加 username

            // 如果有图片，添加图片文件
            if (file) {
                formData.append('avatar', file);
            }

            // 发送POST请求到后端API
            fetch(`${API_BASE_URL}/user/update`, {
                method: 'POST',
                body: formData
            })
                .then(response => {
                    if (!response.ok) throw new Error('网络响应失败');
                    return response.json();
                })
                .then(res => {
                    if (res.code === 1) {
                        alert('个人信息更新成功！');
                        //updateUIWithUserData(res.data); // 更新页面显示
                        location.reload();
                        closeEditModal();
                    } else {
                        alert('更新失败: ' + (res.msg || '请重试'));
                    }
                })
                .catch(error => {
                    console.error('更新失败:', error);
                    alert('更新失败，请检查网络或稍后再试');
                });
        }




        // 头像上传预览
        avatarInput.addEventListener('change', function(e) {
            const file = e.target.files[0];
            if (file) {
                // 简单验证文件类型和大小
                const validTypes = ['image/jpeg', 'image/png', 'image/gif'];
                if (!validTypes.includes(file.type)) {
                    alert("请上传 JPG, PNG 或 GIF 格式的图片！");
                    return;
                }

                if (file.size > 2 * 1024 * 1024) {
                    alert("图片大小不能超过 2MB！");
                    return;
                }

                const reader = new FileReader();
                reader.onload = function(event) {
                    avatarPreview.src = event.target.result;
                };
                reader.readAsDataURL(file);
            }
        });

        // 事件监听
        editProfileBtn.addEventListener('click', openEditModal);
        editInfoBtn.addEventListener('click', openEditModal);
        closeModalBtn.addEventListener('click', closeEditModal);
        cancelEditBtn.addEventListener('click', closeEditModal);
        saveChangesBtn.addEventListener('click', saveChanges);

        // 点击模态框外部关闭
        window.addEventListener('click', function(e) {
            if (e.target === editModal) {
                closeEditModal();
            }
        });

        // 初始化页面
        initUserData();
    });
</script>
</body>
</html>